package com.cgtong.android.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cgtong.android.R;
import com.dyhdyh.widget.loading.dialog.LoadingDialog;
import com.dyhdyh.widget.loading.factory.MaterialDialogFactory;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 普通Fragment父类，不带loading
 * Created by zhuqingchun
 * 2017/9/30 14:50.
 */

public abstract class BaseFragment extends Fragment {

    private Unbinder mUnbinder;

    public abstract int getLayoutId();
    public abstract void initData();


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(getLayoutId(), container, false);
        mUnbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
    }


    /**
     * 显示加载框
     */
    public void doShowLoading(Context context, boolean cancelable) {
        this.doShowLoading(context, null, cancelable);
    }

    public void doShowLoading(Context context, String msg, boolean cancelable) {
        LoadingDialog loadingDialog = LoadingDialog.make(context, new MaterialDialogFactory());
        if (!TextUtils.isEmpty(msg)) {
            loadingDialog.setMessage(msg);//提示消息
        } else {
            loadingDialog.setMessage(getString(R.string.request_net))//提示消息
                    .setCancelable(cancelable);
        }
        loadingDialog.create().show();
    }

    public void doHideLoading() {
        LoadingDialog.cancel();
    }
}
